package com.qinglei.recoup.common.service.impl;

import com.qinglei.recoup.common.exception.RecoupException;
import com.qinglei.recoup.common.service.FileService;
import com.qinglei.recoup.common.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
@Slf4j
public class FileServiceImpl implements FileService {

    @Value("${recoup.oss.pub-img-path}")
    private String ossImgPath;

    @Resource
    private OssService ossService;

    @Override
    public String uploadImage(MultipartFile file) throws IOException, RecoupException {
        String originalFilename = file.getOriginalFilename();
        int pos = originalFilename.lastIndexOf(".");
        String ext = "";
        if (pos != -1) {
            ext = originalFilename.substring(pos);
        }
        String objectName = ossImgPath + "/"+ UUID.randomUUID().toString().replace("-", "") + ext;
        String url = ossService.uploadFile(objectName, file.getInputStream(), true);
        return url;
    }
}
